"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BasicCopyData = void 0;
const FairyGUI = CS.FairyGUI;
const FairyEditor = CS.FairyEditor;
const System = CS.System;
const SG_1 = require("../../SG");
const ToolMgr_1 = require("../../ToolMgr");
const App = FairyEditor.App;
class BasicCopyData {
}
exports.BasicCopyData = BasicCopyData;
class ComBasicPlugin extends FairyEditor.View.PluginInspector {
    static create() {
        //Register a inspector
        App.inspectorView.AddInspector(() => new ComBasicPlugin(), "ComponentBasic", "");
        //Condition to show it
        App.docFactory.ConnectInspector("ComponentBasic", "", false, false);
    }
    //当前输入的值
    //private currentValue: number = 0;
    constructor() {
        super();
        let self = this;
        this.panel = FairyGUI.UIPackage.CreateObject("Extend", "ComponentBasic").asCom;
        this.btnCopy = this.panel.GetChild("btnCopy").asButton;
        this.btnPaste = this.panel.GetChild("btnPaste").asButton;
        this.btnCopy.onClick.Add(this.onBtnCopyClick.bind(this));
        this.btnPaste.onClick.Add(this.onBtnPasteClick.bind(this));
        this.updateAction = () => { return this.updateUI(); };
        if (SG_1.default.config[SG_1.Config.XYWHComputer]) {
            let comBasic = App.inspectorView.GetInspector("basic").panel;
            let items = ["x", "y", "width", "height"];
            for (let index = 0; index < items.length; index++) {
                const element = items[index];
                let inputText = comBasic.GetChild(element).asLabel.GetChild("title").asTextInput;
                //comBasic.GetChild(element).asLabel.GetChild("title").asTextInput.RemoveEventListeners()
                //inputText.onFocusOut.Add(this.onXYWHComputer.bind(this, element))
                // inputText.onChanged.AddCapture(()=>{
                //     this.currentValue = parseFloat(inputText.text)
                // })
                //捕获失去焦点事件 自定义计算
                inputText.onFocusOut.AddCapture((event) => {
                    event.StopPropagation();
                    self.onXYWHComputer(element);
                });
            }
        }
    }
    onXYWHComputer(name) {
        return __awaiter(this, void 0, void 0, function* () {
            let comBasic = App.inspectorView.GetInspector("basic").panel;
            let label = comBasic.GetChild(name).asLabel;
            let text = label.GetTextField().text;
            yield ToolMgr_1.default.sleep(0);
            try {
                let num = eval(text);
                num = Math.floor(num);
                //开启了保持比例
                if (comBasic.GetChild("aspectLocked").asButton.selected && (name == "width" || name == 'height')) {
                    let w = App.activeDoc.inspectingTarget.GetProperty("width");
                    let h = App.activeDoc.inspectingTarget.GetProperty("height");
                    if (name == 'width') {
                        if (num == 0) {
                            App.activeDoc.inspectingTarget.docElement.SetProperty('height', 0);
                        }
                        else {
                            let height = Math.floor(num / w * h);
                            App.activeDoc.inspectingTarget.docElement.SetProperty('height', height);
                        }
                    }
                    else {
                        if (num == 0) {
                            App.activeDoc.inspectingTarget.docElement.SetProperty('width', 0);
                        }
                        else {
                            let width = Math.floor(num / h * w);
                            App.activeDoc.inspectingTarget.docElement.SetProperty('width', width);
                        }
                    }
                }
                App.activeDoc.inspectingTarget.docElement.SetProperty(name, num);
            }
            catch (_a) {
                App.consoleView.Log("请输入数字或者正确的运算符");
            }
        });
    }
    onBtnCopyClick() {
        // let comBasic = App.inspectorView.GetInspector("comBasic").panel;
        let comBasic = App.inspectorView.GetInspector("basic").panel;
        let data = new BasicCopyData();
        data.x = comBasic.GetChild("x").asLabel.GetTextField().text;
        data.y = comBasic.GetChild("y").asLabel.GetTextField().text;
        data.width = comBasic.GetChild("width").asLabel.GetTextField().text;
        data.height = comBasic.GetChild("height").asLabel.GetTextField().text;
        data.scaleX = comBasic.GetChild("scaleX").asLabel.GetTextField().text;
        data.scaleY = comBasic.GetChild("scaleY").asLabel.GetTextField().text;
        data.skewX = comBasic.GetChild("skewX").asLabel.GetTextField().text;
        data.skewY = comBasic.GetChild("skewY").asLabel.GetTextField().text;
        data.pivotX = comBasic.GetChild("pivotX").asLabel.GetTextField().text;
        data.pivotY = comBasic.GetChild("pivotY").asLabel.GetTextField().text;
        data.alpha = comBasic.GetChild("alpha").asLabel.GetTextField().text;
        data.rotation = comBasic.GetChild("rotation").asLabel.GetTextField().text;
        if (App.activeDoc.content.GetController("showRestrictSize")) {
            data.minWidth = comBasic.GetChild("minWidth").asLabel.GetTextField().text;
            data.maxWidth = comBasic.GetChild("maxWidth").asLabel.GetTextField().text;
            data.minHeight = comBasic.GetChild("minHeight").asLabel.GetTextField().text;
            data.maxHeight = comBasic.GetChild("maxHeight").asLabel.GetTextField().text;
        }
        data.useSourceSize = comBasic.GetChild("useSourceSize").asButton.selected;
        data.aspectLocked = comBasic.GetChild("aspectLocked").asButton.selected;
        data.anchor = comBasic.GetChild("anchor").asButton.selected;
        data.visible = !comBasic.GetChild("visible").asButton.selected;
        data.touchable = !comBasic.GetChild("touchable").asButton.selected;
        data.grayed = comBasic.GetChild("grayed").asButton.selected;
        this.copyData = data;
        this.btnPaste.grayed = false;
        this.btnPaste.touchable = true;
    }
    onBtnPasteClick() {
        if (this.copyData == null)
            return;
        let List = ToolMgr_1.default.createGenericList(FairyEditor.FObject);
        List.AddRange(App.activeDoc.inspectingTargets);
        let count = List.Count;
        for (let index = 0; index < count; index++) {
            const obj = List.get_Item(index);
            obj.docElement.SetProperty("scaleX", this.copyData.scaleX.toString());
            obj.docElement.SetProperty("scaleY", this.copyData.scaleY.toString());
            obj.docElement.SetProperty("skewX", this.copyData.skewX.toString());
            obj.docElement.SetProperty("skewY", this.copyData.skewY.toString());
            obj.docElement.SetProperty("pivotX", this.copyData.pivotX.toString());
            obj.docElement.SetProperty("pivotY", this.copyData.pivotY.toString());
            obj.docElement.SetProperty("alpha", this.copyData.alpha.toString());
            obj.docElement.SetProperty("rotation", this.copyData.rotation.toString());
            if (App.activeDoc.content.GetController("showRestrictSize")) {
                obj.docElement.SetProperty("minWidth", this.copyData.minWidth.toString());
                obj.docElement.SetProperty("maxWidth", this.copyData.maxWidth.toString());
                obj.docElement.SetProperty("minHeight", this.copyData.minHeight.toString());
                obj.docElement.SetProperty("maxHeight", this.copyData.maxHeight.toString());
            }
            //开启全部属性赋值
            if (SG_1.default.config[SG_1.Config.CopyAttribute]) {
                obj.docElement.SetProperty("useSourceSize", this.copyData.useSourceSize);
                obj.docElement.SetProperty("aspectLocked", this.copyData.aspectLocked);
                obj.docElement.SetProperty("visible", this.copyData.visible);
                obj.docElement.SetProperty("grayed", this.copyData.grayed);
                obj.docElement.SetProperty("touchable", this.copyData.touchable);
            }
            obj.docElement.SetProperty("width", this.copyData.width.toString());
            obj.docElement.SetProperty("height", this.copyData.height.toString());
            obj.docElement.SetProperty("anchor", this.copyData.anchor);
            obj.docElement.SetProperty("x", this.copyData.x.toString());
            obj.docElement.SetProperty("y", this.copyData.y.toString());
        }
    }
    updateUI() {
        //返回false 不渲染插件
        // let self = this;
        // let List = ToolMgr.createGenericList<CS.FairyEditor.FObject>(FairyEditor.FObject)
        // List.AddRange(App.activeDoc.inspectingTargets);
        // let sels = List;
        // for (let index = 0; index < List.Count; index++) {
        //     const element = List.get_Item(index);
        //     if ((element instanceof FairyEditor.) == false) {
        //         return false
        //     }
        // }
        if (this.panel.parent.GetChild("basic") == null)
            return false;
        let basic = this.panel.parent.GetChild('basic').asCom;
        if (basic == null)
            return false;
        //自定义名称扩展
        if (SG_1.default.config[SG_1.Config.OPENCUSTOMNAME]) {
            let c_name = basic.GetChild("name").asComboBox;
            let list = ToolMgr_1.default.createGenericList(System.String);
            let array = SG_1.default.config[SG_1.Config.CUSTOMNAME];
            array.forEach(element => {
                list.Add(element);
            }, this);
            c_name.values = list.ToArray();
            c_name.items = list.ToArray();
            c_name.text = App.activeDoc.inspectingTarget.name;
        }
        if (App.activeDoc.inspectingTarget instanceof CS.FairyEditor.FTextField) {
            let target = App.activeDoc.inspectingTarget;
            if (target.name == "@lookTextPath") {
                if (target.text == "") {
                    target.text = SG_1.default.config[SG_1.Config.LookTextPath];
                }
                else {
                    SG_1.default.config[SG_1.Config.LookTextPath] = target.text;
                    ToolMgr_1.default.saveConfig(SG_1.default.config_path);
                }
            }
        }
        this.btnPaste.grayed = this.copyData == null;
        this.btnPaste.touchable = this.copyData != null;
        let basicIndex = this.panel.parent.GetChildIndex(basic);
        let customIndex = this.panel.parent.GetChildIndex(this.panel);
        if (basicIndex == customIndex - 1)
            return true;
        let line = this.panel.parent.GetChildAt(customIndex - 1);
        this.panel.parent.SetChildIndex(this.panel, basicIndex + 1);
        if (line)
            line.visible = false;
        return true;
    }
    /**比较数组是否相同 */
    isArrayEqual(a, b) {
        if (a.Length !== b.Length) {
            return false;
        }
        for (let index = 0; index < a.Length; index++) {
            const ai = a.get_Item(index);
            const bi = b.get_Item(index);
            if (ai !== bi)
                return false;
        }
        return true;
    }
}
exports.default = ComBasicPlugin;
